package com.branches.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;

/**
 * @author wujian
 * @date 2024/7/5
 */
public class CalSta<T> {


    public static List<Double> findOutliers(List<Double> data) {
        double mean = calculateMean(data);
        double stdDev = calculateStandardDeviation(data, mean);
        double threshold = 0.8 * stdDev;
        List<Double> outliers = new ArrayList<>();

        for (double value : data) {
            if (Math.abs(value - mean) > threshold) {
                outliers.add(value);
            }
        }

        return outliers;
    }

    public static double calculateMean(List<Double> data) {
        data.sort(Double::compareTo);
        int count =  (int)Math.ceil(data.size() * 0.35);
        data = data.subList(0, Math.min(count, data.size()));
        double sum = 0;
        for (double value : data) {
            sum += value;
        }
        return sum / data.size();
    }

    public static double calculateStandardDeviation(List<Double> data, double mean) {
        double sum = 0;
        for (double value : data) {
            sum += Math.pow(value - mean, 2);
        }
        return Math.sqrt(sum / data.size());
    }



    public static void main(String[] args) {
        List<Double> values = Arrays.asList(684.0, 777.0, 683.0, 759.0, 659.0, 830.0, 760.0, 783.0, 736.0, 779.0, 734.0, 2815.0, 863.0, 673.0,
                723.0, 805.0, 741.0, 1938.0, 787.0, 734.0, 753.0, 782.0, 735.0, 694.0, 2996.0, 814.0, 798.0, 797.0, 752.0, 853.0,
                725.0, 790.0, 646.0, 689.0, 686.0, 748.0, 753.0, 827.0, 684.0, 781.0, 712.0, 686.0, 848.0, 795.0, 725.0, 817.0, 748.0,
                785.0, 687.0, 687.0, 781.0, 752.0, 753.0, 675.0, 800.0, 778.0, 672.0, 763.0, 733.0, 733.0, 751.0, 745.0, 741.0, 704.0, 702.0, 790.0, 690.0, 805.0,
                704.0, 718.0, 732.0, 766.0, 789.0, 754.0, 808.0, 1588.0, 788.0, 805.0, 4337.0, 703.0, 742.0, 652.0, 765.0, 691.0, 808.0, 3178.0, 789.0, 683.0,
                4915.0, 764.0, 813.0, 821.0, 723.0, 811.0, 683.0, 751.0, 675.0, 728.0, 751.0, 835.0, 777.0, 1351.0, 703.0, 797.0, 709.0, 834.0, 749.0, 806.0);
        findOutliers(values);
    }

}
